﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Data.Common;
using TSV.Library.Common;
using TSV.Models;


namespace TSV.Areas.Administration.Models
{
    public class GalleryDao : BaseDao
    {
        public List<Gallery> GetListByPartnerId(int partnerId)
        {
            return dbContext.Galleries.Where(c => c.PartnerID == partnerId).OrderBy(c=> c.ImageOrder).ToList<Gallery>();
        }

        public Gallery GetById(int Id)
        {
            return dbContext.Galleries.Where(c => c.ID == Id).FirstOrDefault<Gallery>();
        }

        /// <summary>
        /// Get last order
        /// </summary>
        /// <returns></returns>
        public int GetLastOrder()
        {
            int lastOrder = 1;
            try
            {
                lastOrder = dbContext.Galleries.OrderByDescending(c => c.ImageOrder).Select(c => c.ImageOrder).FirstOrDefault<int>();
            }
            catch (Exception)
            {

            }
            return lastOrder;
        }

        public Message UpdateActive(int id)
        {
            Message msg = null;
            try
            {

                // Get current group in dbContext
                Gallery objDb = GetById(id);

                if (objDb != null)
                {
                    // Update info by objUI
                    if (objDb.IsShownFrontEnd)
                    {
                        objDb.IsShownFrontEnd = false;
                    }
                    else
                    {
                        objDb.IsShownFrontEnd = true;
                    }
                    // Submit changes to dbContext
                    dbContext.SubmitChanges();
                    // Show success message
                    msg = new Message(MessageConstants.I0001, MessageType.Info, "Hình ", "cập nhật");


                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }

            return msg;
        }

        public Gallery GetByOrder(int order)
        {
            return dbContext.Galleries.Where(c => c.ImageOrder == order).FirstOrDefault<Gallery>();
        }

        public Message MoveUp(Gallery objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    // Get current group in dbContext
                    Gallery objCurrent = GetByOrder(objUI.ImageOrder);
                    Gallery objNext = GetByOrder(objUI.ImageOrder + 1);

                    if (objCurrent != null && objNext != null)
                    {
                        int currentOrder = objCurrent.ImageOrder;
                        int nextOrder = objNext.ImageOrder;
                        // Update info by objUI
                        objCurrent.ImageOrder = nextOrder;
                        objNext.ImageOrder = currentOrder;

                        // Submit changes to dbContext
                        dbContext.SubmitChanges();
                        // Show success message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, "Hình ảnh ", "cập nhật thứ tự");

                    }
                    else
                    {
                        // Show system error
                        msg = new Message(MessageConstants.E0007, MessageType.Error);
                    }
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }

            return msg;
        }

        public Message MoveDown(Gallery objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    // Get current group in dbContext
                    Gallery objCurrent = GetByOrder(objUI.ImageOrder);
                    Gallery objPrevious = GetByOrder(objUI.ImageOrder - 1);

                    if (objCurrent != null && objPrevious != null)
                    {
                        int currentOrder = objCurrent.ImageOrder;
                        int nextOrder = objPrevious.ImageOrder;
                        // Update info by objUI
                        objCurrent.ImageOrder = nextOrder;
                        objPrevious.ImageOrder = currentOrder;

                        // Submit changes to dbContext
                        dbContext.SubmitChanges();
                        // Show success message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, "Hình ảnh ", "cập nhật thứ tự");

                    }
                    else
                    {
                        // Show system error
                        msg = new Message(MessageConstants.E0007, MessageType.Error);
                    }
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }

            return msg;
        }

        public Message Insert(Gallery objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    objUI.ImageOrder = GetLastOrder() + 1;
                    dbContext.Galleries.InsertOnSubmit(objUI);
                    dbContext.SubmitChanges();

                    // Show success message
                    msg = new Message(MessageConstants.I0001, MessageType.Info, "Hình ảnh ", "thêm");
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }

        public Message DeleteList(string ids)
        {
            Message msg = null;
            DbTransaction trans = null;
            try
            {
                dbContext.Connection.Open();
                trans = dbContext.Connection.BeginTransaction();
                dbContext.Transaction = trans;

                if (!string.IsNullOrEmpty(ids))
                {
                    List<Gallery> list = new List<Gallery>();
                    ids = ids.TrimEnd(',');
                    int galleryID = 0;

                    string[] idArr = ids.Split(',');
                    int total = idArr.Count();
                    List<string> imageList = new List<string>();
                    foreach (string id in idArr)
                    {
                        bool isValid = Int32.TryParse(id, out galleryID);
                        Gallery gallery = GetById(galleryID);
                        if (gallery != null)
                        {
                            imageList.Add(gallery.Image);
                            list.Add(gallery);
                        }
                        else
                        {
                            total--;
                        }
                    }
                    if (list.Count > 0)
                    {
                        dbContext.Galleries.DeleteAllOnSubmit(list);
                        dbContext.SubmitChanges();
                        // Show succes message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, total.ToString() + " hình ảnh", "xóa");
                        trans.Commit();

                        //Delete image
                        foreach (string img in imageList)
                        {
                            if (!string.IsNullOrEmpty(img) && img != Constants.NO_IMAGE)
                            {
                                string strPath = HttpContext.Current.Server.MapPath(Constants.PHOTO_PATH_ROOT_PARTNER) + img;
                                //strPath += "\\" + image;
                                if (System.IO.File.Exists(strPath))
                                    System.IO.File.Delete(strPath);
                            }
                        }
                    }
                    else
                    {
                        trans.Rollback();
                    }
                }
            }
            catch (Exception)
            {
                if (trans != null) trans.Rollback();
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }
    }
}